import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './plugins/element.js'
import VueCookie from 'vue-cookie'
// 导入字体图标
import './assets/fonts/iconfont.css'
// 导入全局样式表
import './assets/css/global.css'

// 导入 NProgress 包对应的JS和CSS
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

import axios from 'axios'


axios.defaults.baseURL = 'http://127.0.0.1:8099/'


function getCookie(c_name) {
    if (document.cookie.length > 0) {
        let c_start = document.cookie.indexOf(c_name + "=")
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1
            let c_end = document.cookie.indexOf(";", c_start)
            if (c_end == -1) c_end = document.cookie.length
            return unescape(document.cookie.substring(c_start, c_end))
        }
    }
    return ""
}
// 在 request 拦截器中，展示进度条 NProgress.start()
// axios的拦截器
axios.interceptors.request.use(config => {

    NProgress.start()

    // jwt令牌
    var token = getCookie("token");
    // window.console.log(token);
    if (token != undefined) {
        config.headers['Authorization'] = token; // 让每个请求携带自定义token 请根据实际情况自行修改
    }
    return config
}, error => {
    // Do something with request error
    Promise.reject(error)
})



// 在 response 拦截器中， 隐藏进度条 NProgress.done()
// 接口错误拦截
axios.interceptors.response.use(function(response) {

    NProgress.done()

    let res = response.data;
    let path = location.hash;
    if (res.code == undefined) {
        return response;
    }

    // 处理CommonResult逻辑
    if (res.code == 200) {
        return res;
    } else if (res.code == 401 || res.code == 600) { //|| res.code==401 || res.code==403 
        if (path != '#/index') {

            window.location.href = '/#/login';
        }
        return Promise.reject(res);
    } else {
        Vue.prototype.$message.warning(res.message);
        return Promise.reject(res);
    }
}, (error) => {
    Vue.prototype.$message.error(error.message);
    return Promise.reject(error);
});

Vue.prototype.$http = axios
Vue.use(VueCookie);

Vue.config.productionTip = false

new Vue({
    router,
    render: h => h(App)
}).$mount('#app')